package com.sy.service.impl;

import com.sy.bean.EnterpriseCategory;
import com.sy.bean.query.EnterpriseCategoryTree;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.mapper.EnterpriseCategoryMapper;
import com.sy.service.EnterpriseCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EnterpriseCategoryServiceImpl extends BaseServiceImpl<EnterpriseCategory, EnterpriseCategoryMapper> implements EnterpriseCategoryService {
    @Autowired
    EnterpriseCategoryMapper enterpriseCategoryMapper;
    @Override
    public int save(EnterpriseCategory entity) {
        //根据父类别id查询最大子类编码
        String maxCode = baseMapper.queryMaxCodeByParentId(entity.getParentId());
        if (null==maxCode){
            //不存在最大编码
            //如果父类别id是0，说明是1级类别，则默认设置为101
            if (entity.getParentId()==0){
                //增加的一级类别
                entity.setCode("101");
            }else {
                //非一级类别 根据父类别id查询对应编码
                String tmp = baseMapper.queryCode(entity.getParentId());
                entity.setCode(tmp+"01");
            }
        }else {
            //存在最大编码
            int tmp = Integer.parseInt(maxCode);
            ++tmp;
            entity.setCode(String.valueOf(tmp));
        }
        return super.save(entity);
    }

    @Override
    public List<EnterpriseCategory> query() {
        return enterpriseCategoryMapper.query();
    }

    @Override
    public List<EnterpriseCategoryTree> queryTree() {
        return enterpriseCategoryMapper.queryTree();
    }

}
